import dayjs from 'dayjs'

export function timeFormat(time, format = 'YYYY-MM-DD HH:mm:ss') {
	if (!time) return ''
	if (typeof time === 'string') time = Number(time)
	return dayjs(time).format(format)
}

export function SecondsFormat(ss, divisor = 1) {
	let str = ''
	let s = Number(ss) / divisor
	if (s >= 0) {
		const minutes = Math.floor(s / 60)
		const seconds = Math.floor(s - minutes * 60)
		let mStr = minutes < 10 ? '0' + minutes : minutes
		let sStr = seconds < 10 ? '0' + seconds : seconds
		str = mStr + ':' + sStr
	}
	return str
}


/* 数字 格式化 */
export function nFormatter(num, digits = 2) {
	const si = [
		{ value: 1e18, symbol: 'E' },
		{ value: 1e15, symbol: 'P' },
		{ value: 1e12, symbol: 'T' },
		{ value: 1e9, symbol: 'G' },
		{ value: 1e6, symbol: 'M' },
		{ value: 1e3, symbol: 'k' }
  ]
	for (let i = 0; i < si.length; i++) {
		if (num >= si[i].value) {
			return (
				// (num / si[i].value + 0.1)
				(num / si[i].value)
				.toFixed(digits)
				.replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
			)
		}
	}
	// return num.toString()
	return Number(num).toFixed(digits)
}

export function toFixed(val, digits = 2) {
	return parseFloat(val).toFixed(digits)
}

export function replaceStar(str) {
	return str.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
}
export function replaceStarBank(str) {
	return str.replace(/\d+(\d{4})/, '**** **** **** $1')
}

export function numberFormat(n) {
	let str = n.toString()
	return str[1] ? str : `0${str}`
}
